package cn.iocoder.yudao.module.member.service.siteconfiguration;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.SiteConfigurationVo;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.SysLinkVo;
import cn.iocoder.yudao.module.member.convert.siteconfiguration.MemberDeviceConfigConvert;
import cn.iocoder.yudao.module.member.convert.siteconfiguration.SysLinkConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.siteconfiguration.MemberDeviceConfigDo;
import cn.iocoder.yudao.module.member.dal.dataobject.siteconfiguration.SysLinkDo;
import cn.iocoder.yudao.module.member.dal.mysql.siteconfiguration.SysLinkMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.LINK_REPEAT;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_POINT_NOT_ENOUGH;

@Service
public class SysLinkServiceImpl implements SysLinkService{

    @Resource
    private SysLinkMapper sysLinkMapper;
    @Override
    public void saveConfig(SiteConfigurationVo saveReqVO) {
        if (CollectionUtil.isEmpty(saveReqVO.getSysLinkVos())){
            return;
        }
        Set<String> keySet = new HashSet<String>();
        List<SysLinkDo> sysLinkDos = SysLinkConvert.INSTANCE.convertList(saveReqVO.getSysLinkVos());
        sysLinkDos.forEach(t->{
            t.setId(null);
            if(!keySet.contains(t.getMainCode())){
                keySet.add(t.getMainCode());
            }else {
                throw exception(LINK_REPEAT,t.getMainName());
            }
        });
        sysLinkMapper.insertBatch(sysLinkDos);
    }

    @Override
    public List<SysLinkVo> getConfig() {
        List<SysLinkDo> list = sysLinkMapper.selectList();
        return SysLinkConvert.INSTANCE.convertList2(list);
    }

    /**
     * 更具主表id删除数据
     *
     * @param code
     */
    @Override
    public void deleteBySiteCode(String code) {
        sysLinkMapper.deleteBySiteCode(code);
    }
}
